// 畅通工程

#include <iostream>
#include <cstring>
    using namespace std;
const int maxn = 1000 + 5;
int fa[maxn];
int Find(int x)
{
    //return fa[x]==-1?x:fa[x]=Find(fa[x]);
    if (fa[x] == -1)
        return x;
    return fa[x] = Find(fa[x]);
}
int mix(int x, int y)
{
    int fx = Find(x);
    int fy = Find(y);
    if (fx != fy)
    {
        fa[fx] = fy;
        return 1;
    }
    return 0;
}
int main()
{
    int n, m;
    while (cin >> n, n)
    {
        cin >> m;
        int ans = n - 1;
        memset(fa, -1, sizeof(fa));
        while (m--)
        {
            int x, y;
            cin >> x >> y;
            ans -= mix(x, y);
        }
        cout << ans << endl;
    }
    return 0;
}